<template>
  <div>
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" placeholder="请输入用户名/账号">
        <template v-slot:left></template>
        <template v-slot:right />
      </crudOperation>

      <!--表单渲染-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="560px"
        :append-to-body="true"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="账号" prop="userno">
            <el-input
              size="small"
              :disabled="crud.status.edit == 1"
              v-model="form.userno"
              placeholder="请输入账号"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            <el-input
              size="small"
              v-model="form.username"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input
              size="small"
              v-model="form.email"
              placeholder="请输入邮箱"
            ></el-input>
          </el-form-item>
          <el-form-item label="角色" prop="role">
            <el-select class="w-100" v-model="form.role" placeholder="请选择">
              <template v-for="(item, index) in role_types">
                <el-option
                  :key="index"
                  :label="item.roleName"
                  :value="item.roleKey"
                ></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="短信价格" prop="smsPrice">
            <el-input-number
              :disabled="!checkPermission(permission.user)"
              v-model="form.smsPrice"
              :min="0.1"
              placeholder="短信价格"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <template v-for="(item, idx) in dict.sys_disabled_status">
              <el-radio :key="idx" v-model="form.status" :label="item.value">{{
                item.label
              }}</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="描述" prop="desc">
            <el-input type="textarea" v-model="form.desc"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.btnLoading"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </span>
      </el-dialog>

      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        height="calc(100vh - 180px)"
        border
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="40" />
        <el-table-column label="序号" type="index" width="50">
        </el-table-column>
        <el-table-column prop="username" label="用户名" width="120">
        </el-table-column>
        <el-table-column prop="userno" label="账号" width="120">
        </el-table-column>
        <el-table-column prop="role" label="角色" width="150">
          <template slot-scope="scope">
            {{
              getPubTypeStr(scope.row.role, role_types, "roleKey", "roleName")
            }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="150">
          <template slot-scope="scope">
            <disabled-tag
              :status="scope.row.status"
              :options="dict.sys_disabled_status"
            />
          </template>
        </el-table-column>
        <el-table-column prop="email" label="邮箱" min-width="160">
        </el-table-column>
        <el-table-column prop="desc" label="描述" min-width="180">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="140">
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission">
              <template v-slot:menu>
                <el-button
                  type="text"
                  @click="resetPwdFn(scope.row)"
                  v-permission="permission.reset"
                  >重置密码</el-button
                >
              </template>
            </udOperation>
          </template>
        </el-table-column>
      </el-table>

      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { parseTime } from "@/utils";
import Users, { postUsersResetPwd } from "@http/system/user.js";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import publice from "@/mixins/publice.js";
import { getRoleAll } from "@http/system/role.js";

// crud交由presenter持有
const defaultCrud = CRUD({
  title: "用户",
  url: "api/users/all",
  crudMethod: { ...Users },
  optShow: { status: true, add: true, del: true },
});

const defaultForm = {
  _id: null,
  username: "",
  userno: "",
  email: "",
  role: "",
  desc: "",
  status: "",
  smsPrice: "",
};

export default {
  name: "Users",
  mixins: [
    presenter(defaultCrud),
    header(),
    form(defaultForm),
    crud(),
    publice,
  ],
  dicts: ["sys_disabled_status"],
  components: { pagination, crudOperation, udOperation },
  data() {
    return {
      permission: {
        user: ["admin"],
        list: ["admin", "users:list"],
        add: ["admin", "users:add"],
        edit: ["admin", "users:edit"],
        del: ["admin", "users:del"],
        reset: ["admin", "users:reset"],
      },
      rules: {
        userno: [{ required: true, message: "请输入账号", trigger: "blur" }],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        role: [{ required: true, message: "请选择角色", trigger: "change" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      role_types: [], // 角色
    };
  },
  created() {
    // 获取角色列表
    getRoleAll().then((res) => {
      this.role_types = res.data;
    });
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },

    /**
     * @Author: Songjq
     * @Date: 2021-12-12 12:47:26
     * @Fn: 重置密码
     */
    resetPwdFn(row) {
      this.$confirm("此操作将重置当前用户密码, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //点击确定的操作(调用接口)
          postUsersResetPwd({
            userId: row._id,
          }).then((res) => {
            this.$message.success("操作成功");
          });
        })
        .catch(() => {
          //几点取消的提示
          this.$message("取消操作");
        });
    },
  },
};
</script>
<style lang="scss" scoped></style>
